package com.cyc.lock;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Lock  可重入锁
 */
public class Demo02 {

    public static void main(String[] args) {
        Phone phone = new Phone();
        new Thread(()->{
            phone.call();
        },"A").start();

        new Thread(()->{
            phone.call();
        },"B").start();

    }



}
class Phone2{

    private Lock lock = new ReentrantLock();

    //当send()执行完后才解锁
    public  void call(){
        lock.lock(); //细节问题  第一把锁  lock锁必须配对
        try{
            System.out.println(Thread.currentThread().getName()+"call()");
            send();
        }catch (Exception e){
            e.printStackTrace();
        }
        finally {
            lock.unlock();
        }
    }

    public  void send(){
        lock.lock(); // 第二把锁

        try {
            System.out.println(Thread.currentThread().getName()+"send()");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
}
